#include "UDPReciveThread.h"

UDPReciveThread::UDPReciveThread(UDPProcessThread &process):udpProcess(process)
{
	stop = false;
}

UDPReciveThread::~UDPReciveThread()
{
	Stop();
}
#if 0
int UDPReciveThread::UDPServer(unsigned short port)
{
	udpProcess.UDPServer(port);

	return 0;
}
#endif

int UDPReciveThread::Start()
{
	if (JThread::IsRunning())
	{
		return	-5;
	}
	if (stop_mutex.IsInitialized())
	{
		if (stop_mutex.Init()<0)
		{
			return -1;
		}
	}

	stop = false;
	if (JThread::Start()<0)
	{
		return -2;
	}
	return 0;
}

void UDPReciveThread::Stop()
{
	if (!IsRunning())
	{
		return;
	}
	stop_mutex.Lock();
	stop = true;
	stop_mutex.Unlock();
	if (JThread::IsRunning())
	{
		JThread::Kill();
	}
	stop = false;
}

void* UDPReciveThread::Thread()
{
	int ret;
	JThread::ThreadStarted();
	while (stop == false)
	{
		ret = udpProcess.RecvData();
#if 0	
		if (ret == -1)
		{
			Stop();
			stop = true;
		}
	
		printf("recv data`````````````\n");
#endif
	}
	return NULL;
}
